<template>
	<view class="search">
		<view class="search-box" :style="{marginTop: margintop,height: jn_height}">
			<image class="search-box-img" @click="cli_back" src="https://www.zzzsyh.com/applets/agent/agent/left.png" mode=""></image>
			<view class="search-box-main">
				<image class="search-box-main-img" @click="cli_search" src="https://www.zzzsyh.com/applets/agent/agent/search.png" mode=""></image>
				<input class="search-box-main-input" @confirm="cli_confirm" confirm-type=search v-model="name"placeholder-class="inputplace" placeholder="搜索商品" type="text" value="" />
				<image class="search-box-main-img-two" @click="cli_clear" src="https://www.zzzsyh.com/applets/agent/agent/close_two.png" mode=""></image>
			</view>
		</view>
		<view class="search-main" v-if="!show_list">
			<view class="search-main-head">
				<view class="search-main-head-left">
					搜索历史
				</view>
				<image class="search-main-head-right" @click="cli_clearAll" src="https://www.zzzsyh.com/applets/agent/agent/delete.png" mode=""></image>
			</view>
			<view class="search-main-jl" v-if="!list_search">
				暂无搜索记录
			</view>
			<view class="search-main-list" v-if="list_search">
				<view class="search-main-li" @click="cli_history(item)" v-for="(item, index) of list_search" :key="index">
					{{item}}
				</view>
			</view>
		</view>
		<view class="search-list" v-if="show_list">
			<view class="index-commodict-main-left">
				<view class="index-commodict-main-li" @click="cli_rmtj(item)" v-for="(item, index) of list_hot_left" :key="index">
					<image class="index-commodict-main-li-img" :src=item.partsImages mode=""></image>
					<view class="index-commodict-main-title font_blod">
						{{item.partsName}}
					</view>
					<view class="index-commodict-main-money">
						<view class="index-commodict-main-moneyBox">
							<text class="index-commodict-main-money-txt">￥</text>
							{{item.minPrice}}-{{item.maxPrice}}
							<text class="index-commodict-main-money-unit">/{{item.partsUnit}}</text>
						</view>
						<view class="">
							<text class="index-commodict-main-money-txt2 index-commodict-main-money-txt3" v-if="item.isPost == 1">
								包邮
							</text>
							<text class="index-commodict-main-money-txt2" v-if="item.isPost == 0">
								不包邮
							</text>
						</view>
					</view>
					<view class="index-commodict-main-content" v-if="item.partsHasGrant == 1">
						<text class="index-commodict-main-content-txt1">保</text>
						<text class="index-commodict-main-content-txt2" v-if="item.partsHasGrant == 1">补助金质保</text>
					</view>
				</view>
			</view>
			<view class="index-commodict-main-right">
				<view class="index-commodict-main-li" @click="cli_rmtj(item)" v-for="(item, index) of list_hot_right" :key="index">
					<image class="index-commodict-main-li-img" :src=item.partsImages mode=""></image>
					<view class="index-commodict-main-title font_blod">
						{{item.partsName}}
					</view>
					<view class="index-commodict-main-money">
						<view class="index-commodict-main-moneyBox">
							<text class="index-commodict-main-money-txt">￥</text>
							{{item.minPrice}}-{{item.maxPrice}}
							<text class="index-commodict-main-money-unit">/{{item.partsUnit}}</text>
						</view>
						<view class="">
							<text class="index-commodict-main-money-txt2 index-commodict-main-money-txt3" v-if="item.isPost == 1">
								包邮
							</text>
							<text class="index-commodict-main-money-txt2" v-if="item.isPost == 0">
								不包邮
							</text>
						</view>
					</view>
					<view class="index-commodict-main-content" v-if="item.partsHasGrant == 1">
						<text class="index-commodict-main-content-txt1">保</text>
						<text class="index-commodict-main-content-txt2" v-if="item.partsHasGrant == 1">补助金质保</text>
					</view>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	import allapi from '../api/index.js'
	export default {
		data() {
			return {
				margintop: '', // 距离上边的距离
				jn_height: '',
				show_list: false, // 是否展示搜索内容
				list_search: [],
				list_hot: [],
				name: '',
			}
		},
		onLoad(val) {
			if (val.value) {
				this.name = val.value
				this.cli_search()
				this.show_list =true
			}else if (val.all) {
				this.show_list = true
				this.getPartsList()
			}
		},
		computed: {
			list_hot_left () {
				let list = this.list_hot
				let list1 = []
				list.forEach( (item, index) => {
					if (index % 2 == 0) {
						list1.push(item)
					}
				})
				return list1
			},
			list_hot_right () {
				let list = this.list_hot
				let list1 = []
				list.forEach( (item, index) => {
					if (index % 2 != 0) {
						list1.push(item)
					}
				})
				return list1
			}
		},
		mounted() {
			this.list_search = uni.getStorageSync('list_search')
			// 或许系统屏幕高度
			this.get_uni_head()
			this.getPartsList()
		},
		methods: {
			// 点击返回上个页面
			cli_back () {
				uni.navigateBack();
			},
			// 点击历史
			cli_history (val) {
				this.name = val
			},
			// 删除所有搜索历史
			cli_clearAll () {
				uni.setStorageSync('list_search', [])
				this.list_search = uni.getStorageSync('list_search')
			},
			// 点击搜索
			cli_search () {
				let name = this.name
				if (name) {
					let list = uni.getStorageSync('list_search')
					if (list) {
						list.push(name)
						uni.setStorageSync('list_search', list)
						this.list_search = uni.getStorageSync('list_search')
					}else {
						uni.setStorageSync('list_search', [name])
						this.list_search = uni.getStorageSync('list_search')
					}
					this.show_list = true
					this.getPartsList()
				}else {
					uni.showToast({
						title: '请输入您要搜索的内容',
						icon: 'none'
					})
				}
				
			},
			cli_clear () {
				this.name = ''
				this.list_hot = []
				this.show_list = false
				// this.getPartsList()
			},
			cli_confirm () {
				this.cli_search()
			},
			// 点击热门推荐跳转
			cli_rmtj (item) {
				uni.setStorageSync('factoryId', item.factoryId)
				uni.navigateTo({
					url:'/pages/yuDing/yuDing'
				})
			},
			// 获取热门推荐
			getPartsList () {
				uni.request({
					url: allapi.getPartsList,
					data: {
						name: this.name
					},
					success: (res) => {
						console.log(res);
						this.list_hot = res.data.data
					}
				})
			},
			// 或许系统屏幕高度
			get_uni_head() {
				const systemInfo = uni.getSystemInfoSync();
				let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
				const barheight = menuButtonInfo.height + menuButtonInfo.top + 12
				this.margintop = menuButtonInfo.top + 'px'
				this.jn_height = menuButtonInfo.height + 'px'
				console.log(menuButtonInfo.height);
			}
		}
	}
</script>

<style scoped>
	.search {
		position: absolute;
		width: 100%;
		height: 100%;
		background: #FFFFFF;
		left: 0;
		top: 0;
		display: flex;
		flex-direction: column;
	}
	.search-box {
		/* background: red; */
		display: flex;
		align-items: center;
		margin-right: 200rpx;
		margin-bottom: 12rpx;
	}
	.search-box-img {
		width: 80rpx;
		height: 80rpx;
	}
	.search-box-main {
		flex: 1;
		background: red;
		height: 100%;
		border-radius: 32rpx;
		background: #F3F5F6;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 24rpx;
	}
	.search-box-main-img {
		width: 32rpx;
		height: 32rpx;
	}
	.search-box-main-img-two {
		width: 32rpx;
		height: 32rpx;
	}
	.search-box-main-input {
		/* height: 100%; */
		flex: 1;
		margin: 0 14rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: #333333;
	}
	.search-main {
		flex: 1;
		overflow: auto;
	}
	.search-main-head {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 40rpx;
		margin-top: 56rpx;
	}
	.search-main-head-left {
		font-size: 32rpx;
		font-weight: 400;
		color: #000000;
	}
	.search-main-head-right {
		width: 32rpx;
		height: 32rpx;
	}
	.search-main-jl {		
		font-size: 28rpx;
		color: #AAAAAA;
		font-weight: 400;
		margin-top: 120rpx;
		text-align: center;
	}
	.search-main-list {
		padding: 0 40rpx;
		margin-top: 40rpx;
		/* display: flex; */
	}
	.search-main-li {
		padding: 8rpx 28rpx;		
		background: #F3F5F6;
		font-weight: 400;
		color: #383838;
		line-height: 38rpx;
		font-size: 24rpx;
		float: left;
		margin-right: 24rpx;
		border-radius: 8rpx;
	}
	.search-list {
		padding-right: 24rpx;
		background: #F3F5F6;
		flex: 1;
		overflow: auto;
		display: flex;
	}
	.index-commodict-main-left {
		flex: 1;
	}
	
	.index-commodict-main-right {
		flex: 1;
	}
	
	.index-commodict-main-li {
		width: 308rpx;
		padding: 16rpx 16rpx;
		padding-bottom: 20rpx;
		background: #FFFFFF;
		border-radius: 8rpx;
		margin-left: 22rpx;
		margin-top: 24rpx;
		float: left;
	}
	
	.index-commodict-main-li-img {
		width: 308rpx;
		height: 308rpx;
		border-radius: 12rpx;
		background: #3F536E;
	}
	
	.index-commodict-main-title {
		font-size: 28rpx;
		color: #333333;
		margin-top: 8rpx;
	}
	
	.index-commodict-main-money {
		font-size: 36rpx;
		color: #EF691C;
		line-height: 50rpx;
		margin-top: 8rpx;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
	}
	.index-commodict-main-moneyBox {
		display: block;
		white-space: nowrap;
	}
	.index-commodict-main-money-txt {
		font-size: 24rpx;
	}
	
	.index-commodict-main-money-unit {
		color: #EF691C;
		font-size: 24rpx;
		margin-left: 6rpx;
	}
	
	.index-commodict-main-money-txt2 {
		display: block;
		white-space: nowrap;
		height: 36rpx;
		line-height: 36rpx;
		border: 2rpx solid #EF691C;
		padding: 0 12rpx;
		border-radius: 6rpx;
		font-size: 24rpx;
	}
	.index-commodict-main-money-txt3 {
		color: #28CD50;
		border-color: #28CD50;
	}
	
	.index-commodict-main-content {
		font-size: 24rpx;
		color: #F86B3D;
		height: 52rpx;
		background: #FEEFEA;
		border-radius: 30rpx;
		font-weight: 400;
		margin: 16rpx 0;
		margin-bottom: 0;
		display: flex;
		align-items: center;
		width: 220rpx;
	}
	
	.index-commodict-main-content-txt1 {
		width: 40rpx;
		height: 40rpx;
		line-height: 40rpx;
		font-size: 20rpx;
		background: #F86B3D;
		border-radius: 50%;
		color: #FFFFFF;
		text-align: center;
		margin-left: 12rpx;
	}
	
	.index-commodict-main-content-txt2 {
		flex: 1;
		text-align: center;
	}
	.index-commodict-main-content-txt3 {
		margin-right: 16rpx;
	}
</style>
